<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\User;
use api\wxapp\model\GoodsCate;
use api\wxapp\model\Order;
use api\wxapp\model\Goods;
use api\wxapp\model\OrderList;
use api\wxapp\model\Integral;
class ScoreController extends ApiBaseController
{

    public function index(Goods $goods)
    {
        $params = $this->getParam();
        $this->checkshopid($params);
        $where[] = ['t1.shop_id','eq',$params['shop_id']];

        //搜索条件
        if(isset($params['cate_id'])){
            //此分类是一级分类还是二级分类
            $cate = GoodsCate::get($params['cate_id']);
            if($cate['pid']){
                //当前是二级分类
                $idarr = Goods::where('cateId',$params['cate_id'])->column('id'); 
            }else{
                //一级分类
                $map[] = ['cateId','in',GoodsCate::where('pid',$params['cate_id'])->column('id')];
                $idarr = Goods::where($map)->column('id'); 
            }
            $where[] = ['t1.goods_id','in',$idarr];
        }
        if(isset($params['name'])){
            $where[] = ['t2.name','like','%'.$params['name'].'%'];
        }
        $list = $goods->getScoreList($where);
        $this->success('请求成功!',$list);
    }

    public function order(Order $order)
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $where[] = ['type','eq',2];
        $where[] = ['uid','eq',$params['uid']];
        if(isset($params['status'])){
            $where[] = ['status','eq',$params['status']];
        }
        $list = $order->getList($where);
        $this->success('请求成功!',$list);
    }
    //积分商城商品详情
    public function goodsinfo()
    {
        $params = $this->getParam();
        $this->checkgoodsId($params);
        $this->checkshopid($params);
        $info = Goods::field('id,name,img,para1,content')->get($params['goods_id']);

        $where['shop_id'] = $params['shop_id'];
        $where['goods_id'] = $params['goods_id'];
        $info['num'] = ShopGoods::where($where)->value('num');

        $map[] = ['score','gt',0];
        $map[] = ['goods_id','eq',$params['goods_id']];
        $info['count'] = OrderList::where($map)->SUM('num');
        $this->success('请求成功!',$info);
    }
    //立即兑换
    public function change()
    {
        $params = $this->getParam();
        $this->checkgoodsId($params);
        $this->checkuid($params);
        $this->checkshopid($params);
        !isset($params['pay_price']) ? $this->error('参数缺失!!!!') : '';
        !isset($params['score']) ? $this->error('参数缺失!!!!') : '';
        !isset($params['num']) ? $this->error('参数缺失!!!!') : '';
        !isset($params['freight']) ? $this->error('参数缺失!!!!') : '';

        //查询此商品原价
        $price = ShopGoods::where(['shop_id'=>$params['shop_id'],'goods_id'=>$params['goods_id']])->value('price');
        $insert['score'] = $params['score'];
        $insert['uid'] = $params['uid']; //用户id
        $insert['freight'] = $params['freight'];//运费
        $insert['pay_price'] = $params['pay_price'];//支付价格
        $insert['status'] = 1; //未支付状态
        $insert['addtime'] = $this->returnDateTime();
        $insert['pay_type'] = 3; //支付方式
        $insert['shop_id'] = $params['shop_id'];
        $insert['type'] = 2;//1:常规订单2:积分订单3:秒杀订单
        if(isset($params['remark'])){
            $insert['remark'] = $params['remark']; //订单备注
        }
        $insert['ordernum'] = $this->getOrderNum($params['uid']);
        $insert['goods_price'] = $price*$params['num'];
        $insert['old_price'] = $price*$params['num'];
        Db::startTrans();
        try{
            //添加订单
            $res = Order::insertGetId($insert);
            if(!$res){
                throw new \Exception('订单表储存失败');
            }
            $datas['goods_id'] = $params['goods_id'];
            $datas['num'] = $params['num'];
            $datas['price'] = 0;
            $datas['order_id'] = $res;
            $datas['shop_id'] = $params['shop_id'];
            $datas['old_price'] = $price;
            $datas['score'] = $params['score']/$params['num'];
            $datas['addtime'] = $this->returnDateTime();
            $res1 = OrderList::insert($datas);
            if(!$res1){
                throw new \Exception('订单记录表储存失败');
            }
            Db::commit(); 
        }catch(\Exception $e){
            Db::rollback(); //回滚事务
            $this->error($e->getMessage());
        }
        $this->success('请求成功!',$insert);
    }

}
